package br.com.ustore.agent.backup.domain;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

import javax.swing.SwingUtilities;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import br.com.ustore.agent.backup.producer.AgentBackupService;

public class TestJmsBackupData implements Observer {

	SimplePeerController simplePeerController;

	public TestJmsBackupData() {
		super();
		simplePeerController = new SimplePeerController();
		simplePeerController.addObserver(this); // adiciona essa classe como um observador dos dados
	}

	public void update(Observable arg0, Object arg) {
		SwingUtilities.invokeLater(new Runnable() {

			public void run() {
				ApplicationContext ctx = new ClassPathXmlApplicationContext("app-client.xml");
				AgentBackupService agentBackupService = (AgentBackupService) ctx.getBean("agentBackupService");

				// ler o log
				BufferedReader info = null;
				try {
					info = new BufferedReader(new FileReader(
							"D:/Cesar/eclipse/workspace-jamaica/p2p_client/rateBackupLogger.log"));

					while (info.ready()) {
						String line = info.readLine();

						if (line.contains("INFO")) {
							String[] data = line.split(" ");
							agentBackupService.sendData(Long.parseLong(data[2]), Long.parseLong(data[4]),
									Long.parseLong(data[6]), Integer.parseInt(data[8]), Integer.parseInt(data[10]));
						}
					}
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					try {
						info.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
	}
}
